// pages/course/courseItem/courseItem.js
var util = require("../../../utils/util.js");

Page({
  /**
   * 页面的初始数据
   */
  data: {
    ctlgHidden: true,
    detailHidden: false,
    navsChecked: ["navs-checked", ""],
    detail: {},
    CoverImageUrl: "",
    rlvTeacherList:[],
    rlvTeacherList: [],
    sorts: [
      {
        icon: "(",
        txt: "视频",
        count: 1
      },
      {
        icon: "6",
        txt: "音频",
        count: 2
      },
      {
        icon: "5",
        txt: "直播",
        count: 3
      },
      {
        icon: "=",
        txt: "回放",
        count: 4
      },
      {
        icon: ")",
        txt: "图文",
        count: 5
      },
      {
        icon: "-",
        txt: "线下课程",
        count: 6
      },
    ],
    Count: 6,
    BusinessList: [],
    PackageID:""
  },
  // navs-begin
  onNavsChecked: function (e) {
    var id = parseInt(e.target.dataset.id);
    if (id == 0) {
      this.setData({
        detailHidden: false,
        ctlgHidden: true
      });
    }
    if (id == 1) {
      this.setData({
        detailHidden: true,
        ctlgHidden: false
      });
    }
    var navsChecked = this.data.navsChecked;
    for (let i = 0; i < navsChecked.length; i++) {
      if (id == i) {
        navsChecked[i] = "navs-checked";
      } else {
        navsChecked[i] = "";
      }
    }
    this.setData({
      navsChecked: navsChecked
    });

  },
  // navs-end

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      PackageID:options.PackageID
    });
    // get detail
    var PackageID = options.PackageID;
    var UserID = getApp().globalData.userId;
    var params = {
      Param: {
        PackageID: PackageID,
        UserID: UserID
      },
      Action: 18040
    };
    var page = this;
    util.reqMedia(params, function (value) {
      var detail = page.data.detail;
      detail.VideoTitle = value.Name;
      detail.Price = value.FinallyPrice;
      detail.SaleCount = value.SaleCount;
      detail.TypeName = value.Subject;
      detail.Name = value.UserName;
      detail.HeadPhotoUrl = value.HeadPhotoUrl;
      detail.VideoContent = value.Info;
      detail.ID = value.PackageID;
      page.setData({
        detail: detail,
        CoverImageUrl: value.CoverImageUrl
      });
    });
    //get rltvTeachers
    params = {
      Param: {
        PackageID: PackageID,
      },
      Action: 18045
    };
    util.reqMedia(params, function (value) {
      var UserList = value.UserList;
      for (let i = 0; i < UserList.length; i++) {
        var SubjectItem = UserList[i].SubjectItem;
        // translate nums to strs
        UserList[i].SubjectItem = util.nToS(SubjectItem);
      };
      page.setData({
        rlvTeacherList: UserList
      });
    });
    //get ctlg
    params = {
      Param:{
        Package: PackageID,
        PageIndex:1,
        ShowIndexNum:100
      },
      Action:18030
    };
    var sorts = this.data.sorts;
    util.reqMedia(params,function(value){
      sorts[0]['count'] = value.EduVideoCount;
      sorts[1]['count'] = value.SoundCount;
      sorts[2]['count'] = value.LiveCount;
      sorts[3]['count'] = value.LiveReplayCount;
      sorts[4]['count'] = value.CourseCount;
      sorts[5]['count'] = value.OfflineCourseCount;
      var BusinessList = value.BusinessList;
      page.setData({
        Count:value.Count,
        BusinessList:BusinessList,
        sorts:sorts
      });
    });
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})